/**
 * 
 */
package com.hawkfly.interceptor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wbcs.config.component.application.report.ColBean;
import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.config.component.other.ButtonsBean;
import com.wbcs.jbsf.util.Wbcs4JBSFUtil;
import com.wbcs.system.CacheDataBean;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.assistant.ReportAssistant;
import com.wbcs.system.buttons.AbsButtonType;
import com.wbcs.system.buttons.WbcsButton;
import com.wbcs.system.component.application.report.EditableFormReportType;
import com.wbcs.system.component.application.report.configbean.editablereport.AbsEditableReportEditDataBean;
import com.wbcs.system.component.application.report.configbean.editablereport.EditableReportInsertDataBean;
import com.wbcs.system.intercept.AbsInterceptorDefaultAdapter;
import com.wbcs.util.Consts;

/**
 * 山东传媒网
 * 新闻预览权限拦截
 * @author hawkfly
 */
public class ItprSDCMNewsAuthesCommonDetail extends AbsInterceptorDefaultAdapter
{
    @Override
	public void doStart(ReportRequest rrequest,ReportBean rbean)
    {
        rrequest.authorize(rbean.getId(),Consts.BUTTON_PART,"type{save}","display","false");
        rrequest.authorize(rbean.getId(),Consts.BUTTON_PART,"type{add}","display","false");
        rrequest.authorize(rbean.getId(),Consts.BUTTON_PART,"type{cancel}","display","false");
        rrequest.authorize(rbean.getId(),Consts.BUTTON_PART,"type{reset}","display","false");
        rrequest.authorize(rbean.getId(),Consts.BUTTON_PART,"type{delete}","display","false");
        String rolename = Wbcs4JBSFUtil.getRolenames(rrequest.getRequest().getSession().getAttribute(com.wbcs.jbsf.util.Consts.SESSION_ROLE));
        if("add".equals(rrequest.getStringAttribute(rbean.getId()+"_ACCESSMODE"))){
            rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "publishCstmBtn", "display", "false");
            rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "publishandtoimpttnewsCstmBtn", "display", "false");
            rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "publishandtohomesiteCstmBtn", "display", "false");
            rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "gobackCstmBtn", "display", "false");
            rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "gobackdelCstmBtn", "display", "false");
            rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "revokePublishCstmBtn", "display", "false");
            if(rolename.indexOf("编审") != -1){
                rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "craftCstmBtn", "display", "false");
                rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "checkCstmBtn", "display", "false");
            }else{
                rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "publish4bsRoleCstmBtn", "display", "false");
            }
            //"revokePublishCstmBtn"
        }else if("update".equals(rrequest.getStringAttribute(rbean.getId()+"_ACCESSMODE"))){
            //String rolename = rrequest.getRequest().getParameter("rolename");
            //改为在session中直接获取
            rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "publish4bsRoleCstmBtn", "display", "false");
            if(rolename.indexOf("编辑") != -1){
                rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "craftCstmBtn", "display", "false");
                rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "checkCstmBtn", "display", "false");
            }
            if(rolename.indexOf("编审") != -1){
                rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "craftCstmBtn", "display", "false");
                rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "checkCstmBtn", "display", "false");
            }
        }else{
            String accessmode = rrequest.getRequest().getParameter(rbean.getId()+"_ACCESSMODE");
            if(Consts.READONLY_MODE.equals(accessmode)){//是只读模式
                if("编辑".equals(rolename)){//推送要闻显示
                    
                }else if("编审".equals(rolename)){//推送主站显示
                    
                }else{//将CstmBtn结尾的按钮全部隐藏
                    ButtonsBean bsb = rbean.getButtonsBean();
                    if(bsb!=null){
                        List<AbsButtonType> lstButs = bsb.getAllDistinctButtonsList();
                        for(AbsButtonType btnitem:lstButs)
                        {
                            if(btnitem instanceof WbcsButton){
                               if(btnitem.getName().lastIndexOf("CstmBtn") > 0){
                                    rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, btnitem.getName(), "display", "false");
                               }
                            }
                        }
                    }
                }
            }else{//为newslist 追加动态参数txtxwzt
              if(rolename.indexOf("记者") != -1){
                  rrequest.getRequest().setAttribute("txtxwzt","2");
              }
              if(rolename.indexOf("编审") != -1){
                  rrequest.getRequest().setAttribute("txtpublish","2");
              }
            }
        }
    }
    
    /* 数据相关的权限控制（适用于表单）
     * @see com.wbcs.system.intercept.AbsInterceptorDefaultAdapter#afterLoadData(com.wbcs.system.ReportRequest, com.wbcs.config.component.application.report.ReportBean, java.lang.Object, java.lang.Object)
     */
    @Override
	public Object afterLoadData(ReportRequest rrequest,ReportBean rbean,Object typeObj,Object dataObj)
    {
        if(typeObj instanceof EditableFormReportType)
        {
            String rolename = Wbcs4JBSFUtil.getRolenames(rrequest.getRequest().getSession().getAttribute(com.wbcs.jbsf.util.Consts.SESSION_ROLE));
            //Object bsztObj = ReportDataAssistant.getInstance().getColValue(rrequest,rbean.getId(),"BSZT",((List)dataObj).get(0));
            Object bsztObj = ReportAssistant.getInstance().getPropertyValue(((List)dataObj).get(0), "BSZT");
            //Object xwztObj = ReportDataAssistant.getInstance().getColValue(rrequest,rbean.getId(),"XWZT",((List)dataObj).get(0));
            Object xwztObj = ReportAssistant.getInstance().getPropertyValue(((List)dataObj).get(0), "XWZT");
            if("update".equals(rrequest.getStringAttribute(rbean.getId()+"_ACCESSMODE"))){
                if(rolename.indexOf("编审") != -1){
                    if("0".equals(bsztObj)){
                        rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "gobackdelCstmBtn", "display", "false");
                    }
                }
                
                if(rolename.indexOf("编辑") != -1 || rolename.indexOf("编审") != -1)
                {
                    if("2".equals(xwztObj))
                    {
                        rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "gobackCstmBtn", "display", "false");
                        rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "gobackdelCstmBtn", "display", "false");
                    }
                    
                    if("0".equals(xwztObj) || "1".equals(xwztObj))//编辑、编审角色数据为草稿或送审状态的新闻
                    {
                        rrequest.authorize(rbean.getId(), com.wbcs.util.Consts.BUTTON_PART, "revokePublishCstmBtn", "display", "false");
                    }
                }
            }
        }
        return dataObj;
    }
    /**
     * 处理低版本oracle clob字段 ORA-01461: can bind a LONG value only for insert into a LONG column 问题
     */
    @Override
	public int doSave(ReportRequest rrequest,ReportBean rbean,AbsEditableReportEditDataBean editbean)
    {
        CacheDataBean cdb=rrequest.getCdb(rbean.getId());
        List<Map<String,String>> lstRowData=cdb.getLstEditedData(editbean);
        ColBean xwnrColBean = rbean.getDbean().getColBeanByColColumn("XWNR");
        if(xwnrColBean != null && lstRowData != null)
        {
            for(int i=0;i<lstRowData.size();i++)
            {
                Map<String, String> mrow = lstRowData.get(i);
                String xwnrhtml = mrow.get("XWNR");
                if(xwnrhtml != null){
                    StringBuffer newxwnr = new StringBuffer(xwnrhtml);
                    if(xwnrhtml.length()>=1000 && xwnrhtml.length()<=2000)
                    {
                        int n = 2001-xwnrhtml.length();
                        StringBuffer suffstr = new StringBuffer();
                        for(int j = 0; j <= n; j++)
                        {
                            suffstr.append(" ");
                        }
                        newxwnr.append(suffstr);
                        mrow.put("XWNR",newxwnr.toString());
                    }
                }
            }
        }
        
        if(editbean instanceof EditableReportInsertDataBean)
        {
            List<Map<String, String>> lstInsertData = rrequest.getLstInsertedData(rbean.getId());
            if(lstInsertData != null){
                String ordertimestr = lstInsertData.get(0).get("ordertime");
                ColBean ordertimeColBean = rbean.getDbean().getColBeanByColColumn("ordertime");
                if(ordertimeColBean!=null&&ordertimestr == null)
                {
                    lstInsertData.get(0).put("ordertime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                } 
            }
            
        }
        return super.doSave(rrequest,rbean,editbean);
    }
}

